﻿using App1;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using WordList;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace WordsOfLove
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>

    public class vocBook//单词本
    {
        public string bookName { get; set; }//名字
        public string previewImg { get; set; }//预览图
        public int bookId { get; set; }//Id
        public vocBook(string name,int id,string previewurl= @"https://media-image1.baydn.com/wordmaster_pub_image/tvbuvh/c14a35155abe3568269187d137cdb129.319dd35612efcd30899d03a7091a2b04.jpeg")
        {
            this.bookName = name;
            this.bookId = id;
            this.previewImg = previewurl;
        }
    }
    public class vocNaviIntention//跳转背单词页面的传参
    {
        public vocBook vocBook { get; set; }//学习的单词本
        public bool isReview { get; set; }//跳转的时候是不是启动复习模式
        public vocNaviIntention(vocBook vb, bool ir)
        {
            this.vocBook = vb;
            this.isReview = ir;
        }
    }
    public sealed partial class wordPageWelcome : Page
    {
        public ObservableCollection<vocBook> vocBookLists { get; } = new ObservableCollection<vocBook>();
        public wordPageWelcome()
        {
            this.InitializeComponent();
            initWelCome();
            initVocBookList();
        }
        private void initWelCome()//加载欢迎语
        {
         welcomeTime.Text = DateTime.Now.ToString();
            int hour = DateTime.Now.Hour;
            if (hour >= 6 && hour <= 10)
            {
                welcomeGreeting.Text = "早上好";
            }else if(hour >= 11 && hour <= 14)
            {
                welcomeGreeting.Text = "午安";
            }else if(hour >= 15 && hour <= 17)
            {
                welcomeGreeting.Text = "下午啦";
            }
            else if (hour >= 18 && hour <= 22)
            {
                welcomeGreeting.Text = "晚上咯";
            }
            else
            {
                welcomeGreeting.Text = "辛苦啦";
            }

        }
        public static MySqlConnection GetConnection()//获得数据库连接
        {
            MySqlConnection connection;
            try//先尝试廖宇阳的环境
            {
                connection = new MySqlConnection(
                   "datasource=localhost;username=root;" +
                   "password=rootpwd;database=word;charset=utf8");
                connection.Open();
            }
            catch (Exception err)//LiHaoyang's mysql env
            {
                Debug.WriteLine(err.Message);
                connection = new MySqlConnection(
                "datasource=localhost;username=root;" +
                "password=root;database=dictionary;charset=utf8");
                connection.Open();
            }
            return connection;
        }

        private void vocBookList_ItemClick(object sender, ItemClickEventArgs e)//当选中单词书的时候
        {
            Debug.WriteLine("book sele");
            var seleBook = (vocBook)e.ClickedItem;
            vocNaviIntention newVBIntent = new vocNaviIntention(seleBook, false);//跳转学习模式,所以是false
           // vocBook seleBook = (vocBook)vocBookList.SelectedItem;
            Frame.Navigate(typeof(WordPage), seleBook);//从欢迎界面跳转学习页面
        }
        private void initVocBookList(VocUser user=null)//Load Voc Book List
        {
            using (MySqlConnection conn = GetConnection())
            {
                string sql = "select * from vocbook";
                using (MySqlCommand command = new MySqlCommand(sql, conn))
                {
                    using (MySqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            if (reader.HasRows)
                            {
                                vocBookLists.Add(new vocBook(reader.GetString(0), reader.GetInt32(2),reader.GetString(1)));
                            }
                        }
                    }
                }
                vocBookList.ItemsSource = vocBookLists;
            }
        }
    }
}
